// pages/attendance/monthReport/monthReport.js

const app = getApp()
const networkUtil = app.globalData.networkUtil
const util = app.globalData.util

const logStrPrefix = "attendance.monthReport"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //考勤年月
        isShowPicker_month: false,
        startYear: null,
        endYear: null,
        year: null,
        month: null,
        yearMonthStr: null,

        //月考勤信息
        monthReport: null,
        dayList: [],

    },

    showPicker_month() {
        this.setData({
            isShowPicker_month: true
        })
    },

    onChangeDate(event) {
        //选择考勤月份
        let that = this;
        let _currentMonth = event.detail.currentMonth;
        let ret = _currentMonth.split('-');
        let _year = ret[0];
        let _month = ret[1];
        let _date = _year + '' + _month;
		
        that.setData({
            year: _year,
            month: _month,
            yearMonthStr: _date,
            isShowPicker_month: false
        });
        //获取对应的月考勤信息
        that.getMonthReport();
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this


    },

    /**
     * 绑定文本框的输入事件，获取用户输入的信息，并存入对应的变量中。
     */
    bindInput_common: function(event) {
        let that = this
        let tagId = event.currentTarget.dataset.name
        let tagValue = event.detail.value

        that.setData({
            [tagId]: tagValue
        })
    },

    /**
     * 获取用户月考勤信息。
     */
    getMonthReport: function() {
        let that = this
        let url = app.globalData.baseUrl + "/attendance/getMonthReport"
        let requestData = {
            yearmonth: that.data.yearMonthStr
        }

        networkUtil.request_get(url, requestData).then((res) => {
            //若已获取到monthReport
            that.setData({
                monthReport: res.data.monthReport,
                dayList: res.data.month
            })
            return app.globalData.SUCCESS_STR
        }).catch((res) => {
            //若未获取到monthReport
            console.error(logStrPrefix + ".getMonthReport() 获取月考勤信息,出错!")
            util.showToast("获取月考勤信息,出错!")
            return app.globalData.ERROR_STR
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

        //初始化考勤日期
        let that = this
        let nowDate = new Date();
        let _startYear = nowDate.getFullYear() - 5;
        let _endYear = nowDate.getFullYear();

        that.setData({
            startYear: _startYear,
            endYear: _endYear,
            year: nowDate.getFullYear(),
            month: util.formatNumber(nowDate.getMonth() + 1),
            yearMonthStr: util.formatYearMonth(nowDate)
        });
        //获取对应的月考勤信息
        that.getMonthReport();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})